package jp.co.sopia.custom;

import java.util.ArrayList;
import java.util.List;

import jp.co.sopia.bean.NoteBean;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ListNoteAdapter extends ArrayAdapter<NoteBean> {
	ArrayList<NoteBean> array;
	int resource;
	Context context;

	public ListNoteAdapter(Context context, int textViewResourceId,
			List<NoteBean> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		resource = textViewResourceId;
		array = (ArrayList<NoteBean>) objects;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		CustomView noteview = (CustomView) convertView;
		
		if(noteview == null) noteview = new CustomView(context);
		final NoteBean noteItem = array.get(position);
		if(noteItem != null) {
			TextView workContent = noteview.workContent;
			TextView timeContent = noteview.timeContent;
			CheckBox checkWork = noteview.cb;
			checkWork.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					noteItem.setChecked(isChecked);
				}
			});

			workContent.setText(noteItem.getName());
			timeContent.setText(noteItem.getRemind() + "\n" + noteItem.getDeadline());
			checkWork.setChecked(noteItem.isChecked());
		}
		return noteview;
	}
}
